package com.lxj.xpopup;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.XPopupNoticeShowListener;
import com.lxj.xpopup.util.SharePreferenceUtils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;

/**
 * @author menglei
 */
public class XPopupManager implements Application.ActivityLifecycleCallbacks {

    private static ArrayList<WeakReference<XPopupNoticeShowListener>> weakReferenceArrayList = new ArrayList<>();
    private static HashMap<String, Stack<BasePopupView>> BasePopupViewStack = new HashMap<String, Stack<BasePopupView>>();
    private static boolean isCanShow;
    private static Application application;
    private static ArrayList<Activity> currentActivityWeak = new ArrayList<>();

    public NoPopNoticeLisener noPopNoticeLisener;


    public void setNoPopNoticeLisener(NoPopNoticeLisener noPopNoticeLisener) {
        this.noPopNoticeLisener = noPopNoticeLisener;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {
        if (application == null) {
            application = activity.getApplication();
        }
        currentActivityWeak.add(activity);
    }

    @Override
    public void onActivityPaused(Activity activity) {
        currentActivityWeak.remove(activity);
    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

    public boolean canShow() {
        if (!isCanShow) {
            isCanShow = (boolean) SharePreferenceUtils.get(application, "isCanShow", true);
        }
        return isCanShow;
    }

    private static class XX {
        private static XPopupManager xPopupManager = new XPopupManager();
    }

    private XPopupManager() {

    }

    public static XPopupManager getInstance() {
        return XX.xPopupManager;
    }

    public void registerManager(Application application) {
        XPopupManager.application = application;
        application.registerActivityLifecycleCallbacks(this);
    }

    public Activity getCurrentActivity() {
        try {
            return currentActivityWeak.get(currentActivityWeak.size() - 1);
        } catch (Exception e) {
            return null;
        }
    }

    public int getAcivitySize() {
        return currentActivityWeak.size();
    }


    public void registerShowLisener(XPopupNoticeShowListener xPopupNoticeShowListener) {
        for (WeakReference weakReference : weakReferenceArrayList) {
            if (weakReference != null && weakReference.get() != null) {
                if (weakReference.get() == xPopupNoticeShowListener) {
                    return;
                }
            }
        }
        weakReferenceArrayList.add(new WeakReference<XPopupNoticeShowListener>(xPopupNoticeShowListener));
    }

    public void unregisterShowLisener(XPopupNoticeShowListener xPopupNoticeShowListener) {
        WeakReference<XPopupNoticeShowListener> weakReferenceCopy = null;
        for (WeakReference weakReference : weakReferenceArrayList) {
            if (weakReference != null && weakReference.get() != null) {
                if (weakReference.get() == xPopupNoticeShowListener) {
                    weakReferenceCopy = weakReference;
                }
            }
        }
        if (weakReferenceCopy != null) {
            weakReferenceArrayList.remove(weakReferenceCopy);
        }
        checkNoPopShow();

    }

    public void checkNoPopShow() {
        if (weakReferenceArrayList.size() == 0) {
            if (noPopNoticeLisener != null) {
                noPopNoticeLisener.noPopNotice();
            }
        }
    }

    public void notiveShowAll() {
        for (WeakReference<XPopupNoticeShowListener> weakReference : weakReferenceArrayList) {
            if (weakReference != null && weakReference.get() != null) {
                weakReference.get().noticeShow();
            }
        }
    }

    public void setCanShow(boolean isCanShow) {
        XPopupManager.isCanShow = isCanShow;
        SharePreferenceUtils.put(application, "isCanShow", isCanShow);

    }


    public interface NoPopNoticeLisener {
        void noPopNotice();
    }


}
